import 'dart:core';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
//eventbus的使用1。创建全局对象
final eventBus = EventBus();
void main() {
  runApp(
    MaterialApp(home: TextHomePage()),
  );
}

class TextHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TextHomePageState();
  }
}

class TextHomePageState extends State<TextHomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text("listview"),
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  YjhBtn(),
                  yjhtext(),
                ],
              ),
            )
        )
    );
  }
}
class YjhBtn extends StatelessWidget {
  const YjhBtn({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
        child: Text("按钮"),
        onPressed: (){
          //2.发射事件
          eventBus.fire("你好啊李星河");
        }
    );
  }
}
class yjhtext extends StatefulWidget {
  const yjhtext({Key? key}) : super(key: key);
  @override
  _yjhtextState createState() => _yjhtextState();
}

class _yjhtextState extends State<yjhtext> {
  String message ="data";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //3.监听事件
    eventBus.on<String>().listen((event) {
      print("数据"+event);
      setState(() {
        message =event.toString();
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(message),
    );
  }
}


